#include "SensorCom.h"

extern FrameAPI gl_CurrentFrameAPI;

char gl_Registered;

int gl_TimeToSleep;

int gl_SleepPeriod;

void sendInfoToCoordinator(unsigned char *info, int dataLen)
{
    unsigned char adresseDEST_H[4] = {0x00, 0x00, 0x00, 0x00};
    unsigned char adresseDEST_L[4] = {0x00, 0x00, 0x00, 0x00};
    unsigned char adresseDEST_16Bits[2] = {0xFF, 0xFE};

    makeDataTransmitFrame(info, dataLen, adresseDEST_H, adresseDEST_L, adresseDEST_16Bits);

    sendAPI();

    waitForTransmitStatusPacket();
}

void handleDataPacket(void)
{
    switch(gl_CurrentFrameAPI.content.frame.api.receivePacket.rcvData[0])
    {
        case COORD_REGISTER_RESPONSE : // Register event response
            // Check if response is valid
            if (gl_CurrentFrameAPI.content.frame.api.receivePacket.rcvData[1] == REGISTER_SUCCESS)
            {
                gl_Registered = 1;
                setTimeToSleep(gl_CurrentFrameAPI.content.frame.api.receivePacket.rcvData[2]);
                setSleepPeriod(gl_CurrentFrameAPI.content.frame.api.receivePacket.rcvData[3]);
            }
            else
            {
                gl_Registered = 0; // Frame don't contain a valid response
            }
            break;

        case COORD_DATA_REQUEST : // Transmit data packet
            sendSensorData();
            break;

        default:
            // Unexpected frame received
            break;
    }
}

void setNodeId(unsigned char *nodeId)
{
    unsigned char ATNI[3] = "NI";

    makeATFrame(ATNI, nodeId, 0x52);

    sendAPI();
}

void getNodeId(void)
{
    unsigned char void_parameter[1];

    unsigned char ATNI[3] = "NI";

    void_parameter[0] = '\0';

    makeATFrame(ATNI, void_parameter, 0x52);

    sendAPI();
}

void initSensor(void)
{
    WDTCONbits.SWDTEN = 0;
    gl_Registered = 0;
    gl_TimeToSleep = 0;
    initClock();

    #ifdef SENSOR_PC
        #define PORT_COM_STR "COM3"
        initPortSerie(PORT_COM_STR);
    #else
	initialisationUSART1();
    #endif
}

void registerDevice(void)
{
    int registerRetransmit = 0;
    while((registerRetransmit < 5)&&(gl_Registered == 0))
    {
        sendRequestRegister();
        if((recept(3) != 0) && (gl_CurrentFrameAPI.content.frame.frameType == API_CMD_RECV_PACKET))
        {
            handleDataPacket();
        }
        else
        {
            registerRetransmit++;
        }
    }
    if(registerRetransmit == 5)
    {
        while(1)
        {
            allumerLed(4);
            delaySec(1);
            eteindreLed(4);
            delaySec(1);
        }
    }
}

void unregisterDevice(void)
{
    unsigned char data[1];
    data[0] = SENSOR_UNREGISTER_REQUEST;
    sendInfoToCoordinator(data, 1);
    gl_Registered = 0;
}

void sendRequestRegister(void)
{
    unsigned char data[1];
    // Frame type : Request Register
    data[0] = SENSOR_REGISTER_REQUEST;
    sendInfoToCoordinator(data, 1);
}

void sendSensorData(void)
{
    char iCpt = 1;
    unsigned char data[32];
    data[0] = SENSOR_DATA_PACKET;
    iCpt += getSample(data + 1);
    sendInfoToCoordinator(data, iCpt);
}

int getTimeToSleep(void)
{
    return gl_TimeToSleep;
}

void setTimeToSleep(int tts)
{
    gl_TimeToSleep = tts;
}

int getSleepPeriod(void)
{
    return gl_SleepPeriod;
}

void setSleepPeriod(int sp)
{
    gl_SleepPeriod = sp;
}

int getSample(unsigned char *buff)
{
    buff[0] = '1';
    buff[1] = '2';
    buff[2] = '3';
    buff[3] = '4';
    buff[4] = '5';
    buff[5] = '6';
    return 6;
}
